package org.example.utils;


import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.example.database.Mapper;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * 数据库工具类
 **/

public class SessionUtil {
    private SqlSessionFactory factory;

    /**
     * 用户名
     */
    private String username = null;

    /**
     * 密码
     */
    private String password = null;

    /**
     * 数据库名称
     */
    private String dataName = null;

    /**
     * 数据库驱动
     */
    private String driver = "com.mysql.jdbc.Driver";
    /**
     * 端口
     */
    private int prot = 3306;

    /**
     * 地址
     */
    private String host = "127.0.0.1";

    /**
     * 时区
     */
    private String serverTimezone = "UTC";

    /**
     * 默认构造器
     */
    public SessionUtil() {
    }

    /**
     * 构造器
     *
     * @param username 用户名
     * @param password 密码
     * @param dataName 数据库名称
     * @param host     地址
     * @param prot     端口
     */
    public SessionUtil(String username, String password, String dataName, String host, int prot) {
        this.username = username;
        this.password = password;
        this.dataName = dataName;
        this.host = host;
        this.prot = prot;
    }

    /**
     * 创建 SessionFactory
     */
    public void createFactory() {
        if (dataName == null || username == null) {
            throw new RuntimeException("数据库信息错误");
        }
//        String url = "jdbc:mysql://127.0.0.1:3306/syz?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai";
        String url =
                String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai",
                        host, prot, dataName);

        //获取数据源
        DataSource dataSource = new PooledDataSource(driver, url, username, password);

        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);

        configuration.addMapper(Mapper.class);
        this.factory = new SqlSessionFactoryBuilder().build(configuration);
    }

    public SqlSessionFactory getFactory() {
        if (this.factory == null) {
            createFactory();
        }
        return this.factory;
    }

    /**
     * 获取SqlSession
     *
     * @return
     * @throws IOException
     */
    public SqlSession getSession() throws IOException {
        if (factory == null) {
            createFactory();
        }
        return factory.openSession();
    }

    /**
     * 关闭 session
     *
     * @param session
     * @throws IOException
     */
    public void sessionClose(SqlSession session) throws IOException {
        session.close();
    }
}
